/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.sun.mail.smtp.SMTPMessage;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.CharsetUtil;
/*     */ import com.zimbra.common.util.EmailUtil;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.db.DbOutOfOffice;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.lmtpserver.LmtpCallback;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.IOException;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import java.util.Enumeration;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Session;
/*     */ import javax.mail.Transport;
/*     */ import javax.mail.internet.AddressException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Notification
/*     */   implements LmtpCallback
/*     */ {
/*     */   public static final long DEFAULT_OUT_OF_OFFICE_CACHE_DURATION_MILLIS = 604800000L;
/*     */   private static final int OUT_OF_OFFICE_DIRECT_CHECK_NUM_RECIPIENTS = 10;
/*  77 */   private static final Notification sInstance = new Notification();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private class MimeMessageWithId
/*     */     extends ZMimeMessage
/*     */   {
/*     */     private final String mMessageId;
/*     */     
/*     */ 
/*     */ 
/*     */     private MimeMessageWithId(String messageId)
/*     */     {
/*  91 */       super();
/*  92 */       this.mMessageId = messageId;
/*     */     }
/*     */     
/*     */     protected void updateMessageID() throws MessagingException
/*     */     {
/*  97 */       setHeader("Message-ID", this.mMessageId);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void afterDelivery(Account account, Mailbox mbox, String envelopeSender, String recipientEmail, Message newMessage)
/*     */   {
/*     */     try
/*     */     {
/* 107 */       notifyIfNecessary(account, newMessage, recipientEmail);
/*     */     } catch (MessagingException e) {
/* 109 */       ZimbraLog.mailbox.warn("Unable to send new mail notification", e);
/*     */     } catch (ServiceException e) {
/* 111 */       ZimbraLog.mailbox.warn("Unable to send new mail notification", e);
/*     */     }
/*     */     try
/*     */     {
/* 115 */       outOfOfficeIfNecessary(account, mbox, newMessage, recipientEmail, envelopeSender);
/*     */     } catch (MessagingException e) {
/* 117 */       ZimbraLog.mailbox.warn("Unable to send out-of-office reply", e);
/*     */     } catch (ServiceException e) {
/* 119 */       ZimbraLog.mailbox.warn("Unable to send out-of-office reply", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void forwardWithoutDelivery(Account account, Mailbox mbox, String envelopeSender, String recipientEmail, ParsedMessage pm)
/*     */   {
/*     */     try
/*     */     {
/* 127 */       outOfOfficeIfNecessary(account, mbox, pm, recipientEmail, envelopeSender);
/*     */     } catch (Exception e) {
/* 129 */       ZimbraLog.mailbox.warn("Unable to send out-of-office reply", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public static Notification getInstance() {
/* 134 */     return sInstance;
/*     */   }
/*     */   
/*     */   private void outOfOfficeIfNecessary(Account account, Mailbox mbox, ParsedMessage pm, String rcpt, String envSenderString)
/*     */     throws ServiceException, MessagingException
/*     */   {
/* 140 */     outOfOfficeIfNecessary(account, mbox, pm.getMimeMessage(), null, rcpt, envSenderString);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void outOfOfficeIfNecessary(Account account, Mailbox mbox, Message msg, String rcpt, String envSenderString)
/*     */     throws ServiceException, MessagingException
/*     */   {
/* 148 */     if (msg.inSpam()) {
/* 149 */       ofailed("in spam", null, rcpt, Integer.valueOf(msg.getId()));
/* 150 */       return;
/*     */     }
/* 152 */     if (msg.inTrash()) {
/* 153 */       ofailed("in trash", null, rcpt, Integer.valueOf(msg.getId()));
/* 154 */       return;
/*     */     }
/* 156 */     outOfOfficeIfNecessary(account, mbox, msg.getMimeMessage(), Integer.valueOf(msg.getId()), rcpt, envSenderString);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void outOfOfficeIfNecessary(Account account, Mailbox mbox, MimeMessage mm, Integer msgId, String rcpt, String envSenderString)
/*     */     throws ServiceException, MessagingException
/*     */   {
/* 168 */     boolean replyEnabled = account.isPrefOutOfOfficeReplyEnabled();
/* 169 */     if (ZimbraLog.mailbox.isDebugEnabled()) {
/* 170 */       ZimbraLog.mailbox.debug("outofoffice reply enabled=" + replyEnabled + " rcpt='" + rcpt + "' mid=" + msgId);
/*     */     }
/* 172 */     if (!replyEnabled) {
/* 173 */       return;
/*     */     }
/*     */     
/*     */ 
/* 177 */     Date now = new Date();
/* 178 */     Date fromDate = account.getGeneralizedTimeAttr("zimbraPrefOutOfOfficeFromDate", null);
/* 179 */     if ((fromDate != null) && (now.before(fromDate))) {
/* 180 */       ofailed("from date not reached", null, rcpt, msgId);
/* 181 */       return;
/*     */     }
/* 183 */     Date untilDate = account.getGeneralizedTimeAttr("zimbraPrefOutOfOfficeUntilDate", null);
/* 184 */     if ((untilDate != null) && (now.after(untilDate))) {
/* 185 */       ofailed("until date reached", null, rcpt, msgId);
/* 186 */       return;
/*     */     }
/*     */     
/*     */ 
/* 190 */     if (envSenderString == null) {
/* 191 */       ofailed("envelope sender null", null, rcpt, msgId);
/* 192 */       return;
/*     */     }
/* 194 */     if (envSenderString.length() < 1) {
/* 195 */       ofailed("envelope sender empty", null, rcpt, msgId); return;
/*     */     }
/*     */     
/*     */     javax.mail.internet.InternetAddress envSender;
/*     */     
/*     */     try
/*     */     {
/* 202 */       envSender = new JavaMailInternetAddress(envSenderString, true);
/*     */     } catch (AddressException ae) {
/* 204 */       ofailed("envelope sender invalid", envSenderString, rcpt, msgId, ae);
/* 205 */       return;
/*     */     }
/* 207 */     String destination = envSender.getAddress();
/*     */     
/* 209 */     if (Mime.isAutoSubmitted(mm)) {
/* 210 */       ofailed("auto-submitted not no", destination, rcpt, msgId);
/* 211 */       return;
/*     */     }
/*     */     
/*     */ 
/* 215 */     String[] precedence = mm.getHeader("Precedence");
/* 216 */     if (hasPrecedence(precedence, "bulk")) {
/* 217 */       ofailed("precedence bulk", destination, rcpt, msgId);
/* 218 */       return; }
/* 219 */     if (hasPrecedence(precedence, "junk")) {
/* 220 */       ofailed("precedence junk", destination, rcpt, msgId);
/* 221 */       return; }
/* 222 */     if (hasPrecedence(precedence, "list")) {
/* 223 */       ofailed("precedence list", destination, rcpt, msgId);
/* 224 */       return;
/*     */     }
/*     */     
/*     */ 
/* 228 */     String[] envSenderAddrParts = EmailUtil.getLocalPartAndDomain(destination);
/* 229 */     if (envSenderAddrParts == null) {
/* 230 */       ofailed("envelope sender invalid", destination, rcpt, msgId);
/* 231 */       return;
/*     */     }
/* 233 */     String envSenderLocalPart = envSenderAddrParts[0];
/* 234 */     envSenderLocalPart = envSenderLocalPart.toLowerCase();
/* 235 */     if ((envSenderLocalPart.startsWith("owner-")) || (envSenderLocalPart.endsWith("-owner"))) {
/* 236 */       ofailed("envelope sender has owner- or -owner", destination, rcpt, msgId);
/* 237 */       return;
/*     */     }
/* 239 */     if (envSenderLocalPart.contains("-request")) {
/* 240 */       ofailed("envelope sender contains -request", destination, rcpt, msgId);
/* 241 */       return;
/*     */     }
/* 243 */     if (envSenderLocalPart.equals("mailer-daemon")) {
/* 244 */       ofailed("envelope sender is mailer-daemon", destination, rcpt, msgId);
/* 245 */       return;
/*     */     }
/* 247 */     if (envSenderLocalPart.equals("majordomo")) {
/* 248 */       ofailed("envelope sender is majordomo", destination, rcpt, msgId);
/* 249 */       return;
/*     */     }
/* 251 */     if (envSenderLocalPart.equals("listserv")) {
/* 252 */       ofailed("envelope sender is listserv", destination, rcpt, msgId);
/* 253 */       return;
/*     */     }
/*     */     
/*     */ 
/* 257 */     String ct = mm.getContentType();
/* 258 */     if ((ct != null) && (ct.equalsIgnoreCase("multipart/report"))) {
/* 259 */       ofailed("content-type multipart/report", destination, rcpt, msgId);
/* 260 */       return;
/*     */     }
/*     */     
/*     */ 
/* 264 */     String[] otherAccountAddrs = account.getMultiAttr("zimbraPrefOutOfOfficeDirectAddress");
/* 265 */     if (!AccountUtil.isDirectRecipient(account, otherAccountAddrs, mm, 10)) {
/* 266 */       ofailed("not direct", destination, rcpt, msgId);
/* 267 */       return;
/*     */     }
/*     */     
/*     */ 
/* 271 */     DbPool.DbConnection conn = null;
/*     */     try {
/* 273 */       conn = DbPool.getConnection(mbox);
/* 274 */       if (DbOutOfOffice.alreadySent(conn, mbox, destination, account.getTimeInterval("zimbraPrefOutOfOfficeCacheDuration", 604800000L))) {
/* 275 */         ofailed("already sent", destination, rcpt, msgId); return;
/*     */       }
/*     */     }
/*     */     finally {
/* 279 */       DbPool.quietClose(conn);
/*     */     }
/*     */     
/*     */     try
/*     */     {
/* 284 */       SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
/*     */       
/*     */ 
/* 287 */       out.setFrom(AccountUtil.getFromAddress(account));
/* 288 */       javax.mail.internet.InternetAddress replyTo = AccountUtil.getReplyToAddress(account);
/* 289 */       if (replyTo != null) {
/* 290 */         out.setReplyTo(new Address[] { replyTo });
/*     */       }
/*     */       
/*     */ 
/* 294 */       out.setRecipient(Message.RecipientType.TO, envSender);
/*     */       
/*     */ 
/* 297 */       out.setSentDate(new Date());
/*     */       
/*     */ 
/* 300 */       String subject = Mime.getSubject(mm);
/* 301 */       String replySubjectPrefix = L10nUtil.getMessage(L10nUtil.MsgKey.replySubjectPrefix, account.getLocale(), new Object[0]);
/* 302 */       if (subject == null) {
/* 303 */         subject = replySubjectPrefix;
/* 304 */       } else if (!subject.toLowerCase().startsWith(replySubjectPrefix.toLowerCase())) {
/* 305 */         subject = replySubjectPrefix + " " + subject;
/*     */       }
/* 307 */       String charset = getCharset(account, subject);
/* 308 */       out.setSubject(subject, charset);
/*     */       
/*     */ 
/* 311 */       String messageId = mm.getMessageID();
/* 312 */       if ((messageId != null) && (!messageId.trim().equals(""))) {
/* 313 */         out.setHeader("In-Reply-To", messageId);
/*     */       }
/*     */       
/*     */ 
/* 317 */       out.setHeader("Auto-Submitted", "auto-replied (zimbra; vacation)");
/*     */       
/*     */ 
/* 320 */       out.setHeader("Precedence", "bulk");
/*     */       
/*     */ 
/*     */ 
/* 324 */       if ((account.isPrefOutOfOfficeSuppressExternalReply()) && (isOfExternalSenderType(destination, account, mbox)) && (!isInternalSender(destination, account)))
/*     */       {
/* 326 */         ZimbraLog.mailbox.info(destination + " is external user and no external reply option is set, so no OOO will be sent. ");
/*     */         
/* 328 */         return;
/*     */       }
/* 330 */       boolean sendExternalReply = (account.isPrefOutOfOfficeExternalReplyEnabled()) && (!isInternalSender(destination, account)) && (isOfExternalSenderType(destination, account, mbox));
/*     */       
/* 332 */       String body = account.getAttr(sendExternalReply ? "zimbraPrefOutOfOfficeExternalReply" : "zimbraPrefOutOfOfficeReply", "");
/*     */       
/* 334 */       charset = getCharset(account, body);
/* 335 */       out.setText(body, charset);
/*     */       
/* 337 */       if (Provisioning.getInstance().getConfig().getBooleanAttr("zimbraAutoSubmittedNullReturnPath", true)) {
/* 338 */         out.setEnvelopeFrom("<>");
/*     */       } else {
/* 340 */         out.setEnvelopeFrom(account.getName());
/*     */       }
/*     */       
/* 343 */       MailSender sender = mbox.getMailSender();
/* 344 */       sender.setSaveToSent(false);
/* 345 */       sender.setDsnNotifyOptions(new MailSender.DsnNotifyOption[] { MailSender.DsnNotifyOption.NEVER });
/* 346 */       sender.sendMimeMessage(null, mbox, out);
/* 347 */       ZimbraLog.mailbox.info("outofoffice sent dest='" + destination + "' rcpt='" + rcpt + "' mid=" + msgId);
/*     */       
/*     */       try
/*     */       {
/* 351 */         conn = DbPool.getConnection(mbox);
/* 352 */         DbOutOfOffice.setSentTime(conn, mbox, destination);
/* 353 */         conn.commit();
/*     */       } finally {
/* 355 */         DbPool.quietClose(conn);
/*     */       }
/*     */     } catch (MessagingException me) {
/* 358 */       ofailed("send failed", destination, rcpt, msgId, me);
/*     */     }
/*     */   }
/*     */   
/*     */   private static boolean isInternalSender(String senderAddr, Account account) {
/* 363 */     String[] senderAddrParts = EmailUtil.getLocalPartAndDomain(senderAddr);
/* 364 */     String senderDomain = senderAddrParts[1];
/* 365 */     if (account.getDomainName().equalsIgnoreCase(senderDomain))
/*     */     {
/* 367 */       return true;
/*     */     }
/* 369 */     for (String intDom : account.getInternalSendersDomain()) {
/* 370 */       if (intDom.equalsIgnoreCase(senderDomain)) {
/* 371 */         return true;
/*     */       }
/*     */     }
/* 374 */     return false;
/*     */   }
/*     */   
/*     */   private static boolean isOfExternalSenderType(String senderAddr, Account account, Mailbox mbox) {
/* 378 */     switch (account.getPrefExternalSendersType()) {
/*     */     case ALLNOTINAB: 
/*     */       try {
/* 381 */         return !mbox.index.existsInContacts(Collections.singleton(new com.zimbra.common.mime.InternetAddress(senderAddr)));
/*     */       }
/*     */       catch (IOException e) {
/* 384 */         ZimbraLog.mailbox.error("Failed to lookup contacts", e);
/* 385 */         return true;
/*     */       }
/*     */     case INAB: 
/*     */       try {
/* 389 */         return mbox.index.existsInContacts(Collections.singleton(new com.zimbra.common.mime.InternetAddress(senderAddr)));
/*     */       }
/*     */       catch (IOException e) {
/* 392 */         ZimbraLog.mailbox.error("Failed to lookup contacts", e);
/* 393 */         return true;
/*     */       }
/*     */     }
/*     */     
/* 397 */     return true;
/*     */   }
/*     */   
/*     */   private String getCharset(Account account, String data)
/*     */   {
/* 402 */     String requestedCharset = account.getAttr("zimbraPrefMailDefaultCharset", "utf-8");
/* 403 */     return CharsetUtil.checkCharset(data, requestedCharset);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void notifyIfNecessary(Account account, Message msg, String rcpt)
/*     */     throws MessagingException, ServiceException
/*     */   {
/* 413 */     boolean notify = account.getBooleanAttr("zimbraPrefNewMailNotificationEnabled", false);
/* 414 */     if (!notify) {
/* 415 */       return;
/*     */     }
/*     */     
/*     */ 
/* 419 */     String destination = account.getAttr("zimbraPrefNewMailNotificationAddress");
/* 420 */     if (destination == null) {
/* 421 */       nfailed("destination not set", null, rcpt, msg, null);
/* 422 */       return;
/*     */     }
/*     */     try {
/* 425 */       new JavaMailInternetAddress(destination);
/*     */     } catch (AddressException ae) {
/* 427 */       nfailed("invalid destination", destination, rcpt, msg, ae);
/* 428 */       return;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 433 */     if (msg.inSpam()) {
/* 434 */       nfailed("in spam", destination, rcpt, msg);
/* 435 */       return;
/*     */     }
/*     */     try
/*     */     {
/* 439 */       if (msg.inTrash()) {
/* 440 */         nfailed("in trash", destination, rcpt, msg);
/* 441 */         return;
/*     */       }
/*     */     } catch (ServiceException e) {
/* 444 */       nfailed("call to Message.inTrash() failed", destination, rcpt, msg, e);
/* 445 */       return;
/*     */     }
/*     */     
/*     */ 
/* 449 */     MimeMessage mm = msg.getMimeMessage();
/* 450 */     String[] precedence = mm.getHeader("Precedence");
/* 451 */     if (hasPrecedence(precedence, "bulk")) {
/* 452 */       nfailed("precedence bulk", destination, rcpt, msg);
/* 453 */       return;
/*     */     }
/* 455 */     if (hasPrecedence(precedence, "junk")) {
/* 456 */       nfailed("precedence junk", destination, rcpt, msg);
/* 457 */       return;
/*     */     }
/*     */     
/*     */ 
/* 461 */     String[] autoSubmittedHeaders = mm.getHeader("Auto-Submitted");
/* 462 */     if (autoSubmittedHeaders != null) {
/* 463 */       for (int i = 0; i < autoSubmittedHeaders.length; i++) {
/* 464 */         String headerValue = autoSubmittedHeaders[i].toLowerCase();
/* 465 */         if (headerValue.indexOf("notification") != -1) {
/* 466 */           nfailed("detected a mail loop", destination, rcpt, msg);
/* 467 */           return;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 473 */     String from = account.getAttr("zimbraNewMailNotificationFrom");
/* 474 */     String subject = account.getAttr("zimbraNewMailNotificationSubject");
/* 475 */     String body = account.getAttr("zimbraNewMailNotificationBody");
/* 476 */     if ((from == null) || (subject == null) || (body == null)) {
/* 477 */       nfailed("null from, subject or body", destination, rcpt, msg);
/* 478 */       return;
/*     */     }
/* 480 */     String recipientDomain = getDomain(rcpt);
/*     */     
/* 482 */     Map<String, String> vars = new HashMap();
/* 483 */     vars.put("SENDER_ADDRESS", msg.getSender());
/* 484 */     vars.put("RECIPIENT_ADDRESS", rcpt);
/* 485 */     vars.put("RECIPIENT_DOMAIN", recipientDomain);
/* 486 */     vars.put("NOTIFICATION_ADDRESS", destination);
/* 487 */     vars.put("SUBJECT", msg.getSubject());
/* 488 */     vars.put("NEWLINE", "\n");
/*     */     
/* 490 */     from = StringUtil.fillTemplate(from, vars);
/* 491 */     subject = StringUtil.fillTemplate(subject, vars);
/* 492 */     body = StringUtil.fillTemplate(body, vars);
/*     */     
/*     */     try
/*     */     {
/* 496 */       Session smtpSession = JMSession.getSmtpSession();
/* 497 */       MimeMessage out = new ZMimeMessage(smtpSession);
/* 498 */       out.setHeader("Auto-Submitted", "auto-replied (notification; " + rcpt + ")");
/* 499 */       javax.mail.internet.InternetAddress address = new JavaMailInternetAddress(from);
/* 500 */       out.setFrom(address);
/* 501 */       address = new JavaMailInternetAddress(destination);
/* 502 */       out.setRecipient(Message.RecipientType.TO, address);
/*     */       
/* 504 */       String charset = getCharset(account, subject);
/* 505 */       out.setSubject(subject, charset);
/* 506 */       charset = getCharset(account, body);
/* 507 */       out.setText(body, charset);
/*     */       
/* 509 */       String envFrom = "<>";
/*     */       try {
/* 511 */         if (!Provisioning.getInstance().getConfig().getBooleanAttr("zimbraAutoSubmittedNullReturnPath", true)) {
/* 512 */           envFrom = account.getName();
/*     */         }
/*     */       } catch (ServiceException se) {
/* 515 */         ZimbraLog.mailbox.warn("error encoutered looking up return path configuration, using null return path instead", se);
/*     */       }
/* 517 */       smtpSession.getProperties().setProperty("mail.smtp.from", envFrom);
/*     */       
/* 519 */       Transport.send(out);
/* 520 */       ZimbraLog.mailbox.info("notification sent dest='" + destination + "' rcpt='" + rcpt + "' mid=" + msg.getId());
/*     */     } catch (MessagingException me) {
/* 522 */       nfailed("send failed", destination, rcpt, msg, me);
/*     */     }
/*     */   }
/*     */   
/*     */   private static String getDomain(String address) {
/* 527 */     String[] parts = EmailUtil.getLocalPartAndDomain(address);
/* 528 */     if (parts == null) {
/* 529 */       return null;
/*     */     }
/* 531 */     return parts[1];
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   void interceptIfNecessary(Mailbox mbox, MimeMessage msg, String operation, Folder folder)
/*     */     throws ServiceException
/*     */   {
/* 544 */     Account account = mbox.getAccount();
/* 545 */     String[] interceptAddresses = account.getMultiAttr("zimbraInterceptAddress");
/* 546 */     if (interceptAddresses.length == 0) {
/* 547 */       return;
/*     */     }
/*     */     
/* 550 */     for (String interceptAddress : interceptAddresses) {
/*     */       try {
/* 552 */         ZimbraLog.mailbox.info("Sending intercept of message %s to %s.", new Object[] { msg.getMessageID(), interceptAddress });
/*     */         
/*     */ 
/* 555 */         String folderName = "none";
/* 556 */         String folderId = "none";
/* 557 */         if (folder != null) {
/* 558 */           folderName = folder.getName();
/* 559 */           folderId = Integer.toString(folder.getId());
/*     */         }
/* 561 */         Map<String, String> vars = new HashMap();
/* 562 */         vars.put("ACCOUNT_DOMAIN", getDomain(account.getName()));
/* 563 */         vars.put("ACCOUNT_ADDRESS", account.getName());
/* 564 */         vars.put("MESSAGE_SUBJECT", Mime.getSubject(msg));
/* 565 */         vars.put("OPERATION", operation);
/* 566 */         vars.put("FOLDER_NAME", folderName);
/* 567 */         vars.put("FOLDER_ID", folderId);
/* 568 */         vars.put("NEWLINE", "\r\n");
/*     */         
/* 570 */         String from = StringUtil.fillTemplate(account.getAttr("zimbraInterceptFrom"), vars);
/* 571 */         String subject = StringUtil.fillTemplate(account.getAttr("zimbraInterceptSubject"), vars);
/* 572 */         String bodyText = StringUtil.fillTemplate(account.getAttr("zimbraInterceptBody"), vars);
/*     */         
/*     */ 
/* 575 */         MimeMessage attached = msg;
/* 576 */         boolean headersOnly = account.getBooleanAttr("zimbraInterceptSendHeadersOnly", false);
/* 577 */         if (headersOnly) {
/* 578 */           attached = new MimeMessageWithId(msg.getMessageID(), null);
/* 579 */           Enumeration e = msg.getAllHeaderLines();
/* 580 */           while (e.hasMoreElements()) {
/* 581 */             attached.addHeaderLine((String)e.nextElement());
/*     */           }
/* 583 */           attached.setContent("", msg.getContentType());
/* 584 */           attached.saveChanges();
/*     */         }
/*     */         
/* 587 */         SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
/* 588 */         out.setHeader("Auto-Submitted", "auto-replied (zimbra; intercept)");
/* 589 */         javax.mail.internet.InternetAddress address = new JavaMailInternetAddress(from);
/* 590 */         out.setFrom(address);
/*     */         
/* 592 */         address = new JavaMailInternetAddress(interceptAddress);
/* 593 */         out.setRecipient(Message.RecipientType.TO, address);
/*     */         
/* 595 */         String charset = getCharset(account, subject);
/* 596 */         out.setSubject(subject, charset);
/* 597 */         charset = getCharset(account, bodyText);
/*     */         
/* 599 */         MimeMultipart multi = new ZMimeMultipart();
/*     */         
/*     */ 
/* 602 */         MimeBodyPart part = new ZMimeBodyPart();
/* 603 */         part.setText(bodyText, charset);
/* 604 */         multi.addBodyPart(part);
/*     */         
/*     */ 
/* 607 */         MimeBodyPart part2 = new ZMimeBodyPart();
/* 608 */         part2.setContent(attached, "message/rfc822");
/* 609 */         multi.addBodyPart(part2);
/*     */         
/* 611 */         out.setContent(multi);
/* 612 */         String envFrom = "<>";
/* 613 */         out.setEnvelopeFrom(envFrom);
/*     */         
/* 615 */         out.saveChanges();
/* 616 */         Transport.send(out);
/*     */         
/*     */ 
/* 619 */         multi.removeBodyPart(part2);
/*     */       } catch (MessagingException e) {
/* 621 */         ZimbraLog.lmtp.warn("Unable to send intercept message to %s.", interceptAddress, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static boolean hasPrecedence(String[] precedence, String value)
/*     */   {
/* 631 */     if (precedence != null) {
/* 632 */       for (int i = 0; i < precedence.length; i++) {
/* 633 */         if (precedence[i].equalsIgnoreCase(value)) {
/* 634 */           return true;
/*     */         }
/*     */       }
/*     */     }
/* 638 */     return false;
/*     */   }
/*     */   
/*     */   private static void failed(String op, String why, String destAddr, String rcptAddr, Message msg, Exception e) {
/* 642 */     failed(op, why, destAddr, rcptAddr, Integer.valueOf(msg.getId()), e);
/*     */   }
/*     */   
/*     */   private static void failed(String op, String why, String destAddr, String rcptAddr, Integer msgId, Exception e) {
/* 646 */     StringBuffer sb = new StringBuffer(128);
/* 647 */     sb.append(op).append(" not sent (");
/* 648 */     sb.append(why).append(")");
/* 649 */     sb.append(" mid=").append(msgId);
/* 650 */     sb.append(" rcpt='").append(rcptAddr).append("'");
/* 651 */     if (destAddr != null) {
/* 652 */       sb.append(" dest='").append(destAddr).append("'");
/*     */     }
/* 654 */     ZimbraLog.mailbox.info(sb.toString(), e);
/*     */   }
/*     */   
/*     */   private static void nfailed(String why, String destAddr, String rcptAddr, Message msg, Exception e) {
/* 658 */     failed("notification", why, destAddr, rcptAddr, msg, e);
/*     */   }
/*     */   
/*     */   private static void nfailed(String why, String destAddr, String rcptAddr, Message msg) {
/* 662 */     failed("notification", why, destAddr, rcptAddr, msg, null);
/*     */   }
/*     */   
/*     */   private static void ofailed(String why, String destAddr, String rcptAddr, Integer msgId, Exception e) {
/* 666 */     failed("outofoffice", why, destAddr, rcptAddr, msgId, e);
/*     */   }
/*     */   
/*     */   private static void ofailed(String why, String destAddr, String rcptAddr, Integer msgId) {
/* 670 */     failed("outofoffice", why, destAddr, rcptAddr, msgId, null);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/Notification.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */